package week13.queue.memory;

import week13.queue.api.Consumer;
import week13.queue.api.ConsumerGroup;
import week13.queue.api.Offset;
import week13.queue.api.Storage;

public class MemoryConsumer implements Consumer {
    private Storage storage;
    private ConsumerGroup consumerGroup;
    private Offset offset;

    public MemoryConsumer(Storage storage, ConsumerGroup consumerGroup, Offset offset) {
        this.storage = storage;
        this.consumerGroup = consumerGroup;
        this.offset = offset;
    }

    @Override
    public Object poll() {
        return storage.get(offset.read(consumerGroup));
    }

    @Override
    public void ack(long pos) {
        offset.save(consumerGroup, pos);
    }
}
